package com.psl.services.work.list;

import com.psl.Dao.Conn;
import com.psl.model.custom;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CustomServices {
    public List customlist() {
        Connection conn = new Conn().getConn();
        ResultSet rs = null;
        PreparedStatement pstmt = null;
        List cust = new ArrayList();
        try {
            pstmt = conn.prepareStatement("SELECT * from  customer");
            rs = pstmt.executeQuery();
            while (rs.next()) {
                custom cu = new custom();
                cu.setC_id(rs.getInt(1));
                cu.setC_name(rs.getString(2));
                cu.setC_addr(rs.getString(3));
                cu.setC_tel(rs.getString(4));
                cu.setC_phone(rs.getString(5));
                cu.setC_email(rs.getString(6));
                cu.setC_contact(rs.getString(7));
                cu.setC_country(rs.getString(8));
                cu.setC_status(rs.getInt(9));
                cust.add(cu);
                HttpServletRequest request = ServletActionContext.getRequest();
                request.setAttribute("cust", cust);
            }
            return cust;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                if (pstmt != null) {
                    pstmt.close();
                }
                if (rs != null) {
                    rs.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public List Acustom(int c_id) {
        Connection conn = new Conn().getConn();
        ResultSet rs = null;
        PreparedStatement pstmt = null;
        List cust = new ArrayList();
        try {
            pstmt = conn.prepareStatement("SELECT * from  customer WHERE c_id=?");
            pstmt.setInt(1, c_id);
            rs = pstmt.executeQuery();
            if (rs.next()) {
                custom cu = new custom();
                cu.setC_id(rs.getInt(1));
                cu.setC_name(rs.getString(2));
                cu.setC_addr(rs.getString(3));
                cu.setC_tel(rs.getString(4));
                cu.setC_phone(rs.getString(5));
                cu.setC_email(rs.getString(6));
                cu.setC_contact(rs.getString(7));
                cu.setC_country(rs.getString(8));
                cu.setC_status(rs.getInt(9));
                cust.add(cu);
            }
            return cust;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                if (pstmt != null) {
                    pstmt.close();
                }
                if (rs != null) {
                    rs.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public boolean add(String c_name, String c_addr, String c_tel, String c_phone, String c_email, String c_contact, String c_country, String c_status) {
        Connection conn = new Conn().getConn();
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement("insert into customer(c_name,c_addr,c_tel,c_phone,c_email,c_contact,c_country,c_status) VALUES(?,?,?,?,?,?,?,?)");
            pstmt.setString(1, c_name);
            pstmt.setString(2, c_addr);
            pstmt.setString(3, c_tel);
            pstmt.setString(4, c_phone);
            pstmt.setString(5, c_email);
            pstmt.setString(6, c_contact);
            pstmt.setString(7, c_country);
            pstmt.setString(8, c_status);
            pstmt.execute();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();

        } finally {
            try {
                if (pstmt != null) {
                    pstmt.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    public boolean del(int c_id) {
        Connection conn = new Conn().getConn();
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement("DELETE from customer where c_id=?");
            pstmt.setInt(1, c_id);
            pstmt.execute();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (pstmt != null) {
                    pstmt.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    public boolean update(String c_name, String c_addr, String c_tel, String c_phone, String c_email, String c_contact, String c_country, int c_status, int c_id) {
        Connection conn = new Conn().getConn();
        ResultSet rs = null;
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement("UPDATE  customer set c_name=?,c_addr=?,c_tel=?,c_phone=?,c_email=?,c_contact=?,c_country=?,c_status=? WHERE c_id=?");
            pstmt.setString(1, c_name);
            pstmt.setString(2, c_addr);
            pstmt.setString(3, c_tel);
            pstmt.setString(4, c_phone);
            pstmt.setString(5, c_email);
            pstmt.setString(6, c_contact);
            pstmt.setString(7, c_country);
            pstmt.setInt(8, c_status);
            pstmt.setInt(9, c_id);
            pstmt.executeUpdate();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (pstmt != null) {
                    pstmt.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return true;
    }
}
